/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  ReadContentByTagName.java                                      
 *  File Contents Outline :  日志分段                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.filter.logfilter;

import cn.edu.thu.log.filter.logfilter.URLAbstraction;
import cn.edu.thu.log.read.LogBuffer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import storage.dbhelper.ActivityAbstractHelper;
import storage.dbhelper.LogBodyHelper;
import storage.dbhelper.LogHeadHelper;
import storage.dbobject.DbActivityAbstractRule;
import storage.dbobject.DbLogBody;

/**
 *
 * @author wan
 */
public class ReadContentByTagName {

    private ArrayList<String> headtagList = null;
    private Map<String, String> bodytagMap = null;
    private ArrayList<String> classifyTagList = null;
    private Map<String, String> headLocationMap = null;
    private Map<String,DbActivityAbstractRule> activitypool =null;
    private Map<String,DbLogBody>  logbodypool = null;
    

    public ReadContentByTagName() {
        classifyTagList=new ArrayList<String>();
        bodytagMap = new HashMap<String, String>();
        logbodypool = new HashMap<String,DbLogBody>();
        activitypool = new HashMap<String,DbActivityAbstractRule>();
    }

    public String getAndCutContentByTagName(String tagname, LogBuffer record) {
//        System.out.println("tagname:"+tagname+" record:"+record);
        /**
         * getLogHeadTagList()需要根据record调用？可以改为静态函数？
         */
        if(headtagList == null) {   
            headtagList = record.getLogHeadTagList();   
         //   System.out.println("logheadtaglist"+headtagList);
        }
        if(headLocationMap == null){
            headLocationMap = LogHeadHelper.getLogHeadsLocationMap();
         //    System.out.println("headlocationmap"+headLocationMap);
        }
//        System.out.println("tagname"+tagname);
        if (headtagList.contains(tagname)) {      
            if( headLocationMap.get(tagname) == null ){
                System.out.println("headtaglist:"+headtagList.toString()+" headLocationMap:"+headLocationMap.toString()+" tagname :"+tagname);
            }
            int indexInLogHeadContent = Integer.parseInt(headLocationMap.get(tagname));  
   
            String headcontent = record.getLogHeadContent().get(indexInLogHeadContent-1).toString();        
//            System.out.println("logheadContent:"+record.getLogHeadContent());
//            System.out.println("此字段是日志头");
//            System.out.println("返回内容是："+headcontent);
            if (tagname.equals("Referer")&&classifyTagList.contains(tagname)) {
           //     System.out.println("Referer内容：" + headcontent);
                if (headcontent.equals("") || headcontent.equals("null")) {
                    return headcontent;
                } else {
                    String partheadContent = URLAbstraction.getPartURL(headcontent);
                    //修改record
                    record.getLogHeadContent().set(indexInLogHeadContent, partheadContent);
                    return partheadContent;
                }
            }
            //对有分段规则的字段进行分段
            else if (classifyTagList.contains(tagname)) {
//                System.out.println("begin to filter tagname:" + tagname);
               // ActivityAbstractHelper activityAbstractHelper = new ActivityAbstractHelper();
              //  List<DbActivityAbstractRule> activityAbstractRules = activityAbstractHelper.getActivityAbstractRules(DbActivityAbstractRule.cl_name, tagname);
                if(!activitypool.containsKey(tagname)){
                                    ActivityAbstractHelper activityAbstractHelper = new ActivityAbstractHelper();
                                  List<DbActivityAbstractRule> activityAbstractRules = activityAbstractHelper.getActivityAbstractRules(DbActivityAbstractRule.cl_name, tagname);
                                  if(activityAbstractRules.size()> 0 ){
                                      DbActivityAbstractRule xxd = activityAbstractRules.get(0);
                                      activitypool.put(tagname, xxd);
                                  }
                }
                DbActivityAbstractRule activityAbstractRule = activitypool.get(tagname);
                ArrayList<String> classifyRules = activityAbstractRule.getAbstractRuleList();
                String partcontent = "";
                for (String classifyRule : classifyRules) {
//                    System.out.println("classifyRule:" + classifyRule);
                    if (headcontent == null || headcontent.equals("")) {
                        partcontent = headcontent;
                    } else {
                        String[] temp = classifyRule.split("to");                       
                        //如果在这个区间，则返回这个区间规则
                        if (between(temp, headcontent)) {                           
//                            System.out.println("partcontent结果："+classifyRule);
                            partcontent = classifyRule;
                            break;
                        }
                    }
                }
                if (partcontent.equals("")) {
                    partcontent = "else";
                }
//                System.out.println("partcontent:" + partcontent);
                record.getLogHeadContent().set(indexInLogHeadContent, partcontent);
                return partcontent;
            } 
            else {
                return headcontent;
            }
        } 
        
        //tag是日志体
        else {          
            String abbreviation = "";
//            bodytagMap=record.getLogBodyMap();//对应key是tagname，value是缩写             
            if (bodytagMap.get(tagname) != null) {
                abbreviation = bodytagMap.get(tagname);                       
//                System.out.println("abbreviation:"+abbreviation);
            } 
            else {
                  if(!logbodypool.containsKey(tagname)){
                                      LogBodyHelper logBodyH = new LogBodyHelper();
                                      List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, tagname);
                                      //  DbLogBody logBody;
                                    if (logBodys.size() > 0) {
                                        DbLogBody xxl =  logBodys.get(0);
                                    logbodypool.put(tagname, xxl);
                                }
                  }
                    DbLogBody logBody = logbodypool.get(tagname);                    
                    abbreviation = logBody.getAbbreviation();  
                    bodytagMap.put(tagname,abbreviation);

            }      
            String bodycontent = record.getLogBodyMap().get(abbreviation);  
//            System.out.println("tagname:"+tagname+" 获取的内容是：" + bodycontent);
            if (bodycontent==null||bodycontent.equals("") || bodycontent.equals("null")){
                return bodycontent;
            }

            else if((tagname.equals("URL") || tagname.equals("SearchUrl"))&&classifyTagList.contains(tagname)) {
                String partBodyContent = URLAbstraction.getPartURL(bodycontent);
                //修改record
                record.getLogBodyMap().put(abbreviation, partBodyContent);
                return partBodyContent;
            }
            //对有分段规则的字段进行分段
            else if (classifyTagList.contains(tagname)) {
//                System.out.println("begin to filter tagname:" + tagname);
             //   ActivityAbstractHelper activityAbstractHelper = new ActivityAbstractHelper();
       //         List<DbActivityAbstractRule> activityAbstractRules = activityAbstractHelper.getActivityAbstractRules(DbActivityAbstractRule.cl_name, tagname);
                DbActivityAbstractRule activityAbstractRule = activitypool.get(tagname);
                ArrayList<String> classifyRules = activityAbstractRule.getAbstractRuleList();
                String partcontent = "";
                for (String classifyRule : classifyRules) {
//                    System.out.println("classifyRule:" + classifyRule + " bodycontent:" + bodycontent);
                    String[] temp = classifyRule.split("to");                   
                    //如果在这个区间，则返回这个区间规则
                    if(temp[0].equals("@@")&&(temp[1].equals("@@")))
                        return bodycontent;
                    if (between(temp, bodycontent)) {
//                        System.out.println("partcontent结果："+classifyRule);
                        if(temp[0].equals("@@"))
                            partcontent="小于"+temp[1];                       
                        if(temp[1].equals("@@"))
                            partcontent="大于"+temp[0];
                        else 
                            partcontent=classifyRule;
                        break;
                    }
                }
                if (partcontent.equals("")) {
                    partcontent = "else";
                }
                record.getLogBodyMap().put(abbreviation, partcontent);
//                System.out.println(record.getLogBodyMap().get(abbreviation));
                return partcontent;
            } 
            else {
                return bodycontent;
            }
        }
    }

    private boolean between(String[] rule, String content) {
        int bvalue0 = 0;
        int bvalue1 = 0;
        int tagvalueInteger = 0;
        try {
            if (rule[0].equals("@@")) {
                bvalue0 = Integer.MIN_VALUE;
            } else {
                bvalue0 = Integer.parseInt(rule[0]);
            }
            if (rule[1].equals("@@")) {
                bvalue1 = Integer.MAX_VALUE;
            } else {
                bvalue1 = Integer.parseInt(rule[1]);
            }
            tagvalueInteger = Integer.parseInt(content);           
        } catch (Exception e) {
             return false;     
        }  
         if ((bvalue0 < bvalue1) && (bvalue0 < tagvalueInteger) && (tagvalueInteger < bvalue1)) {
//                System.out.println("bvalue0<content<bvalue1");
                return true;
            } 
            else if ((bvalue0 > bvalue1) && (bvalue0 < tagvalueInteger) && (tagvalueInteger < bvalue0)) {
//                System.out.println("bvalue0>content>bvalue1");
                return true;
            }
            else 
                return false;
    }

    /**
     * @return the classifyTagList
     */
    public ArrayList<String> getClassifyTagList() {
        return classifyTagList;
    }

    /**
     * @param classifyTagList the classifyTagList to set
     */
    public void setClassifyTagList(ArrayList<String> classifyTagList) {
        this.classifyTagList = classifyTagList;
          ActivityAbstractHelper activityAbstractHelper = new ActivityAbstractHelper();
          LogBodyHelper logBodyH = new LogBodyHelper();
          System.out.println("Classifly TagList:"+classifyTagList.toString());
          for(String tagname :this.classifyTagList){
               List<DbActivityAbstractRule> activityAbstractRulesss =  activityAbstractHelper.getActivityAbstractRules(DbActivityAbstractRule.cl_name, tagname);
               if(activityAbstractRulesss != null && activityAbstractRulesss.size() > 0 ) {
                   activitypool.put(tagname, activityAbstractRulesss.get(0));
               }          
          }
        if(headLocationMap == null){
            headLocationMap = LogHeadHelper.getLogHeadsLocationMap();
         //    System.out.println("headlocationmap"+headLocationMap);
        }                  
    }
    public static void main(String [] args){
        
    }
}
